package cn.edu.zut.cs.action;

import cn.edu.zut.cs.Business.SaleService;
import cn.edu.zut.cs.Model.Student;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.awt.print.Book;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/StudentController.do")
public class StudentController extends BaseController {

    private static final long serialVersionUID = 1L;

    SaleService service;

    @Override
    public void init() throws ServletException {
        service = CtxUtil.getBean(SaleService.class);
    }

    // 图书列表Action
    public String ListBook(HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("student", service.qureyAll());
        return "ListStudent.jsp";
    }

    // 删除图书Action
    public String Delete(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        request.setAttribute("message", service.deleteStudent(id) > 0 ? "删除成功！" : "删除失败！");
        request.setAttribute("Student", service.qureyAll());
        return "ListStudent.jsp";
    }

    // 多删除图书Action
    public String Deletes(HttpServletRequest request, HttpServletResponse response) {
        String[] ids = request.getParameterValues("ids");
        if (ids!=null&&ids.length > 0) {
            request.setAttribute("message", service.deleteStudent(ids) > 0 ? "删除成功！" : "删除失败！");
        } else {
            request.setAttribute("message", "请选择删除项！");
        }
        request.setAttribute("student", service.qureyAll());
        return "ListStudent.jsp";
    }

    // 添加图书Action
    public String AddBook(HttpServletRequest request, HttpServletResponse response) {
        return "AddStudent.jsp";
    }

    // 保存添加图书Action
    public String AddBookPost(HttpServletRequest request, HttpServletResponse response) {
        try {
            String name = request.getParameter("name");
            String age = request.getParameter("age");
            String college = request.getParameter("college");


            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date admissionDate = simpleDateFormat.parse(request.getParameter("admissionDate"));

            Student entity = new Student();
            //  entity.setId(id);
            entity.setName(name);
            entity.setAge(age);
            entity.setCollege(college);
            entity.setAdmissionDate(admissionDate);

            if (service.insertStudent(entity) > 0) {
                request.setAttribute("model", new Student());
                request.setAttribute("message", "添加成功！");
            } else {
                request.setAttribute("model", entity);
                request.setAttribute("message", "添加失败！");
            }
        } catch (Exception exp) {
            request.setAttribute("message", exp.getMessage());
            exp.printStackTrace();
        }
        return "AddStudent.jsp";
    }

    //编辑图书Action
    public String EditBook(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        Student model=service.getStudentById(id);
        request.setAttribute("model", model);
        return "EditStudent.jsp";
    }

    // 保存编辑图书Action
    public String EditBookPost(HttpServletRequest request, HttpServletResponse response) {
        try {
            int id=Integer.parseInt(request.getParameter("id"));

            String name = request.getParameter("name");
            String age = request.getParameter("age");
            String college = request.getParameter("college");


            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date admissionDate = simpleDateFormat.parse(request.getParameter("admissionDate"));

            Student entity = new Student();
            entity.setId(id);
            entity.setName(name);
            entity.setAge(age);
            entity.setCollege(college);
            entity.setAdmissionDate(admissionDate);
            request.setAttribute("message", service.updateStudent(entity) > 0 ? "更新成功！" : "更新失败！");
            request.setAttribute("model", entity);
        } catch (Exception exp) {
            request.setAttribute("message", exp.getMessage());
            exp.printStackTrace();
        }
        return "EditStudent.jsp";
    }

}
